package com.application.controller;

import com.application.domain.User;
import com.application.domain.request;
import com.application.service.LeaderService;
import com.application.service.RequestService;
import com.application.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.io.ResolverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/users")
@Api(tags = "用户模块")
public class UserController {
    @Resource
    UserService userService;
    @Resource
    LeaderService leaderService;

    @Resource
    RequestService requestService;

    @ApiOperation(value = "保存用户数据")
    @PostMapping
    Result save(@RequestBody User user){
        int save = userService.save(user);
        String mes = save!=0?"保存数据成功":"保存数据失败，请重新尝试";
        return new Result(save!=0?Code.SAVE_OK :Code.SAVE_ERR,save,mes);
    }

    @ApiOperation("根据openid更新用户信息   而且对于选择负责人的用户进行选择")
    @PutMapping
    public Result updateById(@RequestBody User user){
        String mes = null;
        int updateById=0;
        int code;
        //  对于负责人才需要  进行检查
        if("负责人".equals(user.getPosition())){
            int check = leaderService.check(user);
            if(check==0){
                mes = "修改失败，请检查职位或者部门";
                code=Code.UPDATE_ERR;
            }else{
                mes = "修改成功";
                code=Code.UPDATE_OK;
                updateById = userService.updateById(user);
            }
            //对于普通用户不需要
        }else{
            updateById=userService.updateById(user);
            mes = updateById !=0 ? "修改成功":"修改失败，请重新尝试";
            code=updateById !=0 ? Code.UPDATE_OK:Code.UPDATE_ERR;
        }

        return new Result(code,updateById,mes);
    }

    @ApiOperation("根据用户openid来查找")
    @GetMapping("/{openid}")
    public Result getById(@PathVariable String openid){
        User user = userService.selectById(openid);
        String mes = user!=null?"查询成功":"查询失败，请重新尝试";
        System.out.println(mes);
        return new Result(user!=null ? Code.SELECT_OK:Code.SELECT_ERR,user,mes);
    }

    @ApiOperation("查找所有用户信息")
    @GetMapping
    public Result getAll(){
        List<User> users = userService.selectAll();
        String mes =users!=null?"查询成功":"查询失败，请重新尝试";
        return new Result(users!=null?Code.SELECT_OK:Code.SELECT_ERR,users,mes);
    }

    @ApiOperation("根据用户openid删除信息")
    @DeleteMapping("/deleteByOpenid")
    public Result deleteById(@RequestParam String openid){
        int deleteById = userService.deleteById(openid);
        String mes = deleteById!=0?"删除成功":"删除失败，请重新尝试";
        return new Result(deleteById!=0?Code.DELETE_OK:Code.DELETE_ERR,deleteById,mes);
    }

    @ApiOperation("删除全部用户信息")
    @DeleteMapping
    public Result deleteAll(){
        int deleteAll = userService.deleteAll();
        String mes = deleteAll!=0?"删除成功":"删除失败，请重新尝试";
        return new Result(deleteAll!=0?Code.DELETE_OK:Code.DELETE_ERR,deleteAll,mes);
    }

    @ApiOperation("根据多个id值 删除多个用户信息")
    @DeleteMapping("/DeleteByList")
    public Result deleteByIds(@RequestParam List<String> ids){
        int deleteByIds = userService.deleteByIds(ids);
        String mes = deleteByIds!=0?"删除成功":"删除失败，请重新尝试";
        return new Result(deleteByIds!=0?Code.DELETE_OK:Code.DELETE_ERR,deleteByIds,mes);
    }



    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        String userId = userService.Login(user);
        String mes = userId!=null?"登录成功":"登录失败，用户名或密码错误";
        return new Result(userId!=null?Code.LOGIN_OK:Code.LOGIN_ERR,userId,mes);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result register(@RequestBody User user){
       int colm= userService.register(user);
       String userId=userService.getId(user);
        String mes = colm!=0?"注册成功":"注册失败，该用户已经存在";
        return new Result(colm!=0?Code.REGISTER_OK:Code.REGISTER_ERR,userId,mes);
    }


    @ApiOperation("图片确定存入数据库")
    @PostMapping("/download/commit")
    public Result DownLoadCommit(@RequestBody User user){

        int downLoad = userService.DownLoad(user);
        String mes = downLoad!=0?"保存成功":"保存失败，请重新尝试";
        Integer code = downLoad!=0?Code.SAVE_OK:Code.SAVE_ERR;

        return new Result(code,downLoad,mes);
    }

    @GetMapping("/activity")
    @ApiOperation(value = "根据用户openid来查看用户参加的活动")
    public Result getActivityByOpenid(@RequestParam String openid){
        //根据用户的  来查看  request表格
        List<request> activityByOpenid = requestService.getActivityByOpenid(openid);

        String mes = activityByOpenid!=null ? "查看成功":"当前用户没有申请活动";
        int code = activityByOpenid !=null ? Code.SELECT_OK:Code.SELECT_ERR;
        return new Result(code,activityByOpenid,mes);
    }


}
